<template>
  <div class="app-container">
    <div>
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query.queryMap"
          :query-more-json="false"
          :filter-list="filterInfo.searchList"
          :filter-btn="filterInfo.searchBtn"
          :list-type-info="listTypeInfo"
          :clear-type="clearType"
        >
        </NewPageFilter>
      </div>
      <NewPageFilter :filter-btn="filterInfo.btnList">
        <el-button v-show="true" @click="_getSfxxmNow"
          >拉取HIS审核记录</el-button
        >
      </NewPageFilter>
      <table-container
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :check-box="true"
        :tab-index="true"
        :query="filterInfo.query.queryMap"
        :handle="tableInfo.handle"
        :field-list="tableInfo.fieldList"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-hisIdLike="scope">
          <p>{{ scope.row.hisId || '-' }}</p>
        </template>
        <template v-slot:col-nameLike="scope">
          <p>{{ scope.row.name || '-' }}</p>
        </template>
      </table-container>
    </div>
  </div>
</template>

<script setup name="hisSfxxm">
import TableContainer from '@/components/TableContainer/index'
import {
  getSfxxmNow,
  getSfxxmPage,
  postSfxxmClose,
  postSfxxmRetry,
} from '@/api/his'

const { proxy } = getCurrentInstance()
const emit = defineEmits()

// 过滤相关配置
const groupSelectInfo = reactive({
  options: [],
  selections: [],
  data: [],
})
// 查询
const filterInfo = reactive({
  sortArr: [],
  copyQueryMap: {},
  query: {
    queryMap: {},
  },
  searchList: [],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      type: 'button',
      label: '拉取HIS审核记录',
      event: _getSfxxmNow,
      show: true,
    },
  ],
})
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  api: undefined,
  data: [],
  selections: [],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '再次提交',
        show: true,
        event: retry,
        ifRender: ifIsretry,
      },
      {
        label: '关闭',
        show: true,
        event: close,
        ifRender: ifClose,
      },
    ],
  },
})
const listTypeInfo = reactive({
  shbzList: [
    { key: '待审核', value: 0 },
    { key: '已审核', value: 1 },
    { key: '已拒绝', value: 2 },
  ],
})
const advancedSearchInfo = reactive({
  visible: false,
  data: {},
})
const clearType = ref(true)

let obj = {
  hisId: {
    label: '代码',
    value: 'hisIdLike',
    asValue: 'hisId',
    type: 'slot',
    componentType: 'input',
    event: search,
    show: true,
    hidden: false,
  },
  name: {
    label: '名称',
    value: 'nameLike',
    asValue: 'name',
    type: 'slot',
    componentType: 'input',
    event: search,
    show: true,
    hidden: false,
  },
  shbz: {
    label: '状态',
    value: 'shbz',
    componentType: 'select',
    list: 'shbzList',
    event: search,
    show: true,
    hidden: true,
  },
  shbzValue: {
    label: '状态',
    value: 'shbzValue',
    componentType: 'text',
    show: true,
    hidden: false,
  },
  btgyy: {
    label: '拒绝原因',
    value: 'btgyy',
    componentType: 'input',
    show: false,
    hidden: false,
  },
  productCode: {
    label: '产品编码',
    value: 'productCode',
    componentType: 'input',
    show: false,
    hidden: false,
  },
  standard: {
    label: '规格',
    value: 'standard',
    componentType: 'input',
    show: false,
    hidden: false,
  },
  licenseNumber: {
    label: '证号',
    value: 'licenseNumber',
    componentType: 'input',
    show: false,
    hidden: false,
  },
  brandName: {
    label: '厂家名称',
    value: 'brandName',
    show: false,
    hidden: false,
  },
  unitName: {
    label: '单位',
    value: 'unitName',
    slot: true,
    show: false,
    hidden: false,
  },
  defaultSupplierName: {
    label: '供应商',
    value: 'defaultSupplierName',
    slot: true,
    show: false,
    hidden: false,
  },
  defaultSupplierPrice: {
    label: '价格',
    value: 'defaultSupplierPrice',
    componentType: 'price',
    type: 'column',
    slot: true,
    formatter: proxy.$fn.TripartiteMethod,
    show: false,
    hidden: false,
  },
}
tableInfo.fieldList = Object.values({
  ...proxy.$fn.deepClone(obj),
})
filterInfo.searchList = Object.values({
  ...proxy.$fn.deepClone(obj),
})

getCustomize()

function ifClose(row) {
  return row.shbz === 2
}
function ifIsretry(row) {
  return row.isRetry === 1
}
function getIds(row = undefined) {
  let ids = []
  if (row) {
    ids.push(row.id)
  } else {
    ids = tableInfo.selections.map((row) => row.id)
  }
  return ids
}
function tableRowClassName({ row }) {
  let rowId = sessionStorage.getItem('storeRowId')
  if (rowId && row.id === Number(rowId)) {
    return 'check-row'
  }
  return ''
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.selections = [...data]
      break
  }
}
// 获取列表
async function getList() {
  tableInfo.refresh = Math.random()
}
// 搜索
function search() {
  advancedSearchInfo.data = {}
  filterInfo.query.queryDtoList = []
  if (groupSelectInfo.selections.length > 0) {
    getList()
  } else {
    groupSelectInfo.data = []
    tableInfo.initCurpage = Math.random()
    tableInfo.refresh = Math.random()
  }
}
function clearSearch() {
  clearType.value = !clearType.value
  advancedSearchInfo.data = {}
  filterInfo.query.queryMap = proxy.$fn.deepClone(filterInfo.copyQueryMap)
  search()
}
function getCustomize() {
  tableInfo.api = getSfxxmPage
  getList()
}
function close(row) {
  let ids = [row.hisId]
  postSfxxmClose(ids).then((res) => {
    if (res && res.success) {
      getList()
    }
  })
}
function retry(row) {
  let ids = [row.hisId]
  postSfxxmRetry(ids).then((res) => {
    if (res && res.success) {
      getList()
    }
  })
}
function _getSfxxmNow() {
  getSfxxmNow().then((res) => {
    if (res && res.success) {
      getList()
    }
  })
}
</script>

<style scoped></style>
